package org.maitaole.service.impl;

import org.maitaole.entity.EvaluateBaseInfo;
import org.maitaole.mapper.EvaluateBaseInfoMapper;
import org.maitaole.service.IEvaluateBaseInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EvaluateBaseInfoServiceImpl implements IEvaluateBaseInfoService {

    @Resource
    private EvaluateBaseInfoMapper evaluateBaseInfoMapper;

    @Override
    public Map<String, List<EvaluateBaseInfo>> getAll(Integer goodsId) {
        /**
         * map的key是类型，也就是前端需要展示的 "基本信息(1)"、功能情况(2)、"外观成色(3)"
         * map的key是每个类型列表信息。
         */
        Map<String, List<EvaluateBaseInfo>> typeToEvaluateBaseInfo = new HashMap<>();

        List<EvaluateBaseInfo> evaluateBaseInfos = evaluateBaseInfoMapper.getAll(goodsId);

        // [{type: 1, xxx}, {type: 2, xxx}, {type: 1, xxx}, {type: 1, xxx}, {type: 3, xxxx}]

        // {"1": [{}, {}, {}], "2": [], "3": []}
        evaluateBaseInfos.forEach(e -> {
            String type = e.getBaseInfoType() + "";  // 获取类型
//            System.out.println(e.getIsMultiple());
            List<EvaluateBaseInfo> evaluateBaseInfoList = typeToEvaluateBaseInfo.get(type);
            if(null == evaluateBaseInfoList) {
                evaluateBaseInfoList = new ArrayList<>();
                evaluateBaseInfoList.add(e);
                typeToEvaluateBaseInfo.put(type, evaluateBaseInfoList);
            }else {
                evaluateBaseInfoList.add(e);
            }
        });

        return typeToEvaluateBaseInfo;
    }
}
